package com.wac.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wac.data.entity.*;
import com.wac.data.entity.Dynamic;
import com.wac.data.mapper.*;
import com.wac.data.service.CommentService;
import com.wac.data.service.DynamicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wac.data.service.StateService;
import com.wac.data.service.UserService;
import com.wac.data.utils.DataListUtil;
import com.wac.data.utils.UpLoad;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author beta
 * @since 2023-03-16
 */
@Service
public class DynamicServiceImpl extends ServiceImpl<DynamicMapper, Dynamic> implements DynamicService {

	private Map<String, Object> map;
	private Page<Dynamic> thePage;

	@Resource
	private DynamicMapper dynamicMapper;
	@Resource
	private CommentMapper commentMapper;
	@Resource
	private LikeMapper likeMapper;
	@Resource
	private UserService userService;
	@Resource
	private StateService stateService;
	@Resource
	private CommentService commentService;


	//获取动态信息
	private Page getDynamicInfo(Page<Dynamic> thePage) {
		thePage.getRecords().forEach(dynamic -> {
			dynamic.setUser(userService.getUserSimpleInfo(dynamic.getUserId()));
			//获取点赞数和评论数浏览数
			dynamic.setCommentNum(commentMapper.selectCount(new QueryWrapper<Comment>().eq("comment_id", dynamic.getId())).intValue());
			dynamic.setLikeNum(likeMapper.getEntityLikeNum(dynamic.getId()));
		});
		return thePage;
	}

	//插入动态
	@Override
	public boolean addDynamic(Dynamic dynamic, List<MultipartFile> file) {
		/**
		 * 腾讯版云存版 下面是本地版
		 */
		// String url = new TencentCouldCos().uploadfile(file.get(0), 4);
		// if (file.size() > 1) {
		// 	for (int i = 1; i < file.size(); i++) {
		// 		url = url + "," + new TencentCouldCos().uploadfile(file.get(i), 4);
		// 	}
		// }
		if (file != null && file.size() > 0) {
			String url = new UpLoad().upLoadFiles(file, 4).get("url").toString();
			dynamic.setImageUrl(url);
		}
		if (dynamicMapper.insert(dynamic) == 1) {
			return true;
		}
		return false;
	}


	// 获取动态
	@Override
	public Map<String, Object> getDynamic(int page) {
		thePage = new Page<>(page, 5);
		dynamicMapper.selectPage(thePage, new QueryWrapper<Dynamic>().orderByDesc("time"));
		return new DataListUtil().data(getDynamicInfo(thePage));
	}

	@Override
	public Map<String, Object> getDynamicByUserId(int page, String id) {
		thePage = new Page<>(page, 5);
		dynamicMapper.selectPage(thePage, new QueryWrapper<Dynamic>().eq("user_id", id).orderByDesc("time"));
		return new DataListUtil().data(getDynamicInfo(thePage));
	}

	@Override
	public Dynamic getDynamicById(String id, String userId) {
		Dynamic dynamic = dynamicMapper.selectById(id);
		dynamic.setUser(userService.getUserSimpleInfo(dynamic.getUserId()));
		dynamic.setLikeNum(likeMapper.getEntityLikeNum(id));
		dynamic.setLikeNum(likeMapper.getEntityLikeNum(dynamic.getId()));
		stateService.addView(id, 0);
		if (userId != null) {
			dynamic.setIsLike(likeMapper.selectOne(new QueryWrapper<Like>().eq("like_id", userId).eq("entity_id", dynamic.getId())) != null ? 1 : 0);
		}
		return dynamic;
	}

	@Override
	public Map<String, Object> search(String text, int page) {
		thePage = new Page<>(page, 10);
		dynamicMapper.selectPage(thePage, new QueryWrapper<Dynamic>().like("content", text).or().eq("content", text).or().orderByAsc("content"));
		return new DataListUtil().data(getDynamicInfo(thePage));
	}

	@Override
	public boolean delete(String id) {
		String[] url = dynamicMapper.selectById(id).getImageUrl().split(",");
		if (url != null || url.length > 0) {
			for (int i = 0; i < url.length; i++) {
				if (new UpLoad().deleteFile(url[i])) {
					continue;
				}
				return false;
			}
		}
		commentMapper.selectList(new QueryWrapper<Comment>().eq("comment_id", id)).forEach(comment -> {
			commentService.delete(comment.getId());
		});
		if (dynamicMapper.deleteById(id) == 1) {
			return true;
		}
		return false;
	}
}
